/**
 * 转正流程状态渲染对应组件
 */
import React from 'react';
import { Divider, Empty } from 'antd';
import { PageContainer } from '@/molecules';
import { useRequest, defaultFormatResult } from '@/hooks';
import { connect } from 'umi';
// import { history } from 'umi'
import { PersonalCard } from '../containers';
import { getInformalSnapshotById } from '../services';

import GuideHR from './GuideHR';
import GuideTutor from './GuideTutor';
import GuideLeader from './GuideLeader';
import AssessTutorW from './AssessTutorW';
import AssessLeaderW from './AssessLeaderW';
import AssessHRW from './AssessHRW';
import AssessTutorM from './AssessTutorM';
import AssessLeaderM from './AssessLeaderM';
import AssessHRM from './AssessHRM';
import AssessTutorQ from './AssessTutorQ';
import AssessLeaderQ from './AssessLeaderQ';
import AccessHRQ from './AssessHRQ';
import InterviewPreparingW from './InterviewPreparingW';
import InterviewPreparingM from './InterviewPreparingM';
import InterviewPreparingQ from './InterviewPreparingQ';
import InterviewDetail from './InterviewDetail';
import { TRIAL_ACTION } from '../constants';
import PassOld from './PassOld';

const interviewRecordObject = { component: InterviewDetail, title: '面谈详情' };

const PAGE_INFO = {
    [TRIAL_ACTION.GUIDE_HR]: { component: GuideHR, title: '人事引导详情' },
    [TRIAL_ACTION.GUIDE_TEACHER]: { component: GuideTutor, title: '带教引导详情' },
    [TRIAL_ACTION.GUIDE_LEADER]: { component: GuideLeader, title: '主管引导详情' },
    [TRIAL_ACTION.ASSESS_TEACHER_WEEK]: { component: AssessTutorW, title: '带教人周评详情' },
    [TRIAL_ACTION.ASSESS_LEADER_WEEK]: { component: AssessLeaderW, title: '主管周评详情' },
    [TRIAL_ACTION.ASSESS_HR_WEEK]: { component: AssessHRW, title: '人事周评详情' },
    [TRIAL_ACTION.ASSESS_TEACHER_MONTH]: { component: AssessTutorM, title: '带教人月评详情' },
    [TRIAL_ACTION.ASSESS_LEADER_MONTH]: { component: AssessLeaderM, title: '主管月评详情' },
    [TRIAL_ACTION.ASSESS_HR_MONTH]: { component: AssessHRM, title: '人事月评详情' },
    [TRIAL_ACTION.ASSESS_TEACHER_QUARTER]: { component: AssessTutorQ, title: '带教人季评详情' },
    [TRIAL_ACTION.ASSESS_LEADER_QUARTER]: { component: AssessLeaderQ, title: '主管季评详情' },
    [TRIAL_ACTION.ASSESS_HR_QUARTER]: { component: AccessHRQ, title: '人事季评详情' },
    [TRIAL_ACTION.INTERVIEW_READY_WEEK]: { component: InterviewPreparingW, title: '面谈准备' },
    [TRIAL_ACTION.PASS_OLD]: { component: PassOld, title: '转正详情' },
    [TRIAL_ACTION.INTERVIEW_LOG_WEEK]: interviewRecordObject,
    [TRIAL_ACTION.INTERVIEW_SUMMARY_WEEK]: interviewRecordObject,
    [TRIAL_ACTION.INTERVIEW_DETAIL_WEEK]: interviewRecordObject,

    [TRIAL_ACTION.INTERVIEW_READY_MONTH]: { component: InterviewPreparingM, title: '面谈准备' },
    [TRIAL_ACTION.INTERVIEW_LOG_MONTH]: interviewRecordObject,
    [TRIAL_ACTION.INTERVIEW_SUMMARY_MONTH]: interviewRecordObject,
    [TRIAL_ACTION.INTERVIEW_DETAIL_MONTH]: interviewRecordObject,

    [TRIAL_ACTION.INTERVIEW_READY_QUARTER]: { component: InterviewPreparingQ, title: '面谈准备' },
    [TRIAL_ACTION.INTERVIEW_LOG_QUARTER]: interviewRecordObject,
    [TRIAL_ACTION.INTERVIEW_SUMMARY_QUARTER]: interviewRecordObject,
    [TRIAL_ACTION.INTERVIEW_DETAIL_QUARTER]: interviewRecordObject
};

function Process({ dispatch, location }) {
    const { query = {} } = location;
    const { id } = query;
    const { data, loading } = useRequest(() => getInformalSnapshotById({ id }), {
        ...defaultFormatResult,
        refreshDeps: [id]
    });

    const status = query.status || data?.status;

    const pageInfo = PAGE_INFO[status] || {};
    const Component = pageInfo.component;

    // React.useEffect(() => {
    //     if (!data) {
    //         return;
    //     }
    //     const { opEmployeeCode = '' } = data;
    //     const { employeeCode } = user;
    //     if (!opEmployeeCode.includes(employeeCode)) {
    //         message.error('暂无权限！');
    //         history.push('/');
    //     }
    // }, [data, user]);

    React.useEffect(() => {
        if (pageInfo.title) {
            dispatch({
                type: 'menu/updateTabTitle',
                payload: {
                    pathname: location.pathname,
                    title: pageInfo.title
                }
            });
        }
    }, [data]);

    if (!data?.status) {
        return (
            <PageContainer loading={loading}>
                <Empty />
            </PageContainer>
        );
    }

    return (
        <PageContainer loading={loading}>
            <PersonalCard data={data?.employeeInfo} />
            <Divider style={{ margin: '16px 0' }} />
            {Component && <Component data={data} />}
        </PageContainer>
    );
}

export default connect(({ user }) => ({ user: user.currentUser }))(Process);
